package com.wzh.glsc.product.app;

import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;
import com.wzh.glsc.product.entity.AttrEntity;
import com.wzh.glsc.product.entity.AttrGroupEntity;
import com.wzh.glsc.product.service.AttrAttrgroupRelationService;
import com.wzh.glsc.product.service.AttrGroupService;
import com.wzh.glsc.product.service.AttrService;
import com.wzh.glsc.product.service.CategoryService;
import com.wzh.glsc.product.vo.AttrAttrgroupRelationVo;
import com.wzh.glsc.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.omg.CORBA.Object;


/**
 * 属性分组
 *
 * @author lcm
 * @email 1291529969@gmail.com
 * @date 2022-03-18 05:33:17
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 条件查询
     */
    @RequestMapping("/list/{catelogId}")
    public R listByCatelogId(@RequestParam Map<String, Object> params,
                             @PathVariable("catelogId") Long catalogId) {
        PageUtils page = attrGroupService.queryPageByCatalogId(params, catalogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
//        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.getCatelogPathBycatId(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
    /**
     * 分组没有关联的属性
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrGroupId) {
        return R.ok().put("page", attrService.noattrRelation(params,attrGroupId));
    }


    /**
     * 分组关联信息
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId) {
        List<AttrEntity> attrEntity = attrGroupService.getAttrRelation(attrGroupId);
        return R.ok().put("data", attrEntity);
    }

    ///product/attrgroup/{catelogId}/withattr
    //查询分类-》分组->属性 集合
    @RequestMapping("/{catelogId}/withattr")
    public R withattrBycatelogId(@PathVariable("catelogId") Long attrGroupId) {
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos=attrGroupService.withattrBycatelogId(attrGroupId);

        return R.ok().put("data", attrGroupWithAttrsVos);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 保存
     * /product/attrgroup/attr/relation
     */
    @RequestMapping("/attr/relation")
    public R addAttrAttrgroupRelation(@RequestBody AttrAttrgroupRelationVo[] attrAttrgroupRelationVos) {
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationVos);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 属性与分组关联删除
     */
    @RequestMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrAttrgroupRelationVo[] attrAttrgroupRelationVos) {
        attrGroupService.removeRelation(attrAttrgroupRelationVos);
        return R.ok();
    }

}
